package com.xss.animtest;

import android.animation.ArgbEvaluator;
import android.animation.TypeEvaluator;
import android.util.Log;

/**
 * 自定义字符串型估值器
 * Created by Administrator on 2018/9/10.
 */

public class StringEvaluator implements TypeEvaluator {
    private static final String TAG = "xss";
    @Override
    public Object evaluate(float fraction, Object startValue, Object endValue) {
        if (startValue==null||endValue==null){
            throw new NullPointerException("空引用");
        }
        String startStr=startValue.toString();
        String endStr=endValue.toString();
        if (!startStr.equals("")&&!endStr.startsWith(startStr)){
            throw new RuntimeException("endValue应以startValue开头");
        }
        if (endStr.length()<startStr.length()){
            throw new RuntimeException("endValue长度小于startValue长度");
        }
        String addStr=endStr.substring(startStr.length());//添加的字符串
        int n=(fraction+"").substring((fraction+"").indexOf('.')+1).length();
        long numBottom= (long) Math.pow(10,n);
        long numTop= (long) (fraction*numBottom);
        Log.d(TAG, "evaluate: fraction："+fraction+" 分数："+numTop+"/"+numBottom);
        if (fraction!=0f&&numTop*addStr.length()%numBottom==0){
            int addCount= (int) (fraction*addStr.length());
            Log.d(TAG, "evaluate: fraction="+fraction+" 返回："+(startStr+addStr.substring(0,addCount)));
            return startStr+addStr.substring(0,addCount);
        }
        return null;
    }
}
